package ui.activity.mine;

import adapter.DeliveryAreaAdapter;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import base.BaseBean;
import base.activity.BaseLayoutActivity;
import bean.AreaBean;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.dispatchapp.andlei.R;
import netWork.NetUtils;
import utils.GsonUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 配送区域
 *
 * @author Andlei
 * @DATE 2019/1/21
 */
public class DeliveryAreaActivity extends BaseLayoutActivity {
    private List<AreaBean> list = new ArrayList<>();
    private DeliveryAreaAdapter adapter;
    private RecyclerView recyclerView;
    private NetUtils netUtils;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle("选择区域");
        setTitlefontBg_black();
        setBaseRightText("保存",Color.parseColor("#00B89D"));
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_delivery_area;
    }

    @Override
    protected void findViews() {
        netUtils = new NetUtils(mActivity,netRequestCallBack);
        recyclerView = getView(R.id.list_rcy);
//        for(int i = 0; i<10;i++){
//           TestBean bean  = new TestBean();
//           bean.str = "区域"+i;
//           if(i==5){
//               bean.isSelect = true;
//           }
//           list.add(bean);
//       }
    }

    @Override
    protected void formatViews() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(mActivity);
        layoutManager.setOrientation(LinearLayout.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
        adapter = new DeliveryAreaAdapter(R.layout.item_delivert_area,list);
        recyclerView.setAdapter(adapter);
        adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if(view.getId() == R.id.img_look){
                    Bundle bundle = new Bundle();
                    bundle.putString("name",list.get(position).getName());
                    bundle.putString("school_id",""+list.get(position).getArea_id());
                    jumpTo(DeliveryAreaDetailActivity.class,bundle);
                }
            }
        });
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if(list.get(position).getStatus().equals("1")){
                    list.get(position).setStatus("0");
                }else {
                    list.get(position).setStatus("1");
                }
                adapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    protected void formatData() {
        Map<String,Object> map = new HashMap<>();
        netUtils.get("rider/user/area-list",map, BaseBean.class,null,false);
    }

    @Override
    protected void getBundle(Bundle bundle) {

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.base_right_text:
                Map<String,Object> cmap = new HashMap<>();
                String area_ids = "";
                for(AreaBean bean:list){
                    if(bean.getStatus().equals("1")){
                        area_ids+=bean.getArea_id()+",";
                    }
                }
                if(!TextUtils.isEmpty(area_ids)&&area_ids.length()>1){
                    area_ids = area_ids.substring(0,area_ids.length()-1);
                }else {
                    return;
                }
                cmap.put("area_ids",""+area_ids);
                netUtils.post("rider/user/set-area",cmap,BaseBean.class,false);
                break;
        }
    }
    private NetUtils.NetRequestCallBack netRequestCallBack = new NetUtils.NetRequestCallBack() {
        @Override
        public void error(String action, Throwable e, Map tag) {

        }

        @Override
        public void success(String action, BaseBean t, Map tag) {
            if(t.getCode() ==200){
                switch (action){
                    case "rider/user/set-area":
                        toast("保存成功");
                        finish();
                        break;
                    case "rider/user/area-list":
                        list = GsonUtils.getInstance(mActivity).parseNoHeaderJArray(t,AreaBean.class);
                        adapter.setNewData(list);
                        break;
                }
            }else {
                toast(t.getMsg());
            }

        }
    };
}
